package org.hawk.gwt.ppc.introspection.impl;

import org.hawk.gwt.ppc.introspection.JavaArrayTypeScope;
import org.hawk.gwt.ppc.introspection.JavaGenericArrayTypeScope;
import org.hawk.gwt.ppc.introspection.JavaGenericTypeScope;
import org.hawk.gwt.ppc.introspection.JavaScope;
import org.hawk.gwt.ppc.introspection.JavaTypeScope;

/**
 * Represents generic array (relevant to {@link java.lang.reflect.GenericArrayType}).
 * 
 * @author alex.bereznevatiy@gmail.com
 *
 */
final class JavaArrayGenericTypeScopeImpl extends AbstractJavaGenericTypeScope
		implements JavaArrayTypeScope, JavaGenericArrayTypeScope {
	
	private JavaTypeScope component;

	JavaArrayGenericTypeScopeImpl(JavaTypeScope component, JavaScope owner) {
		super(component, owner);
		
		this.component = component;
	}

	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.introspection.JavaArrayTypeScope#getComponentType()
	 */
	public JavaTypeScope getComponentType() {
		return component;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return getComponentType().toString() + "[]";
	}

	@Override
	JavaTypeScope getRaw() {
		JavaTypeScope component = getComponentType();
		if (component instanceof JavaGenericTypeScope) {
			component = ((JavaGenericTypeScope)component).getRawType();
		}
		return new JavaArrayTypeScopeImpl(component);
	}

	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.introspection.JavaTypeScope#isAssignableFrom(org.hawk.gwt.ppc.introspection.JavaTypeScope)
	 */
	public boolean isAssignableFrom(JavaTypeScope from) {
		if (equals(from)) {
			return true;
		}
		if (from instanceof JavaArrayTypeScope) {
			return getComponentType().isAssignableFrom(((JavaArrayTypeScope)from).getComponentType());
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj instanceof JavaArrayTypeScope) {
			return getComponentType().equals(((JavaArrayTypeScope)obj).getComponentType());
		}
		
		return false;
	}
}
